﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EShop.BussinessObjects;
using EShop.DataAccess;
using EShop.Common;

namespace EShop.BusinessLogic
{
    public class CBLOrderMaster
    {
        public static bool InsertOrder(OrderMaster orderMaster, List<Discount> discountList, out string errMsg)
        {
            //TODO : What is the proper way to handle errors ? rollback all the order ?
            bool res = true;
            errMsg = string.Empty;

            List<Discount> discountListNew = new List<Discount>(discountList);
            foreach (Discount discount in discountList)
            {
                if (discount.DiscountDiscountElementID == cnstDiscountElement.Product)
                {
                    OrderDetail orderDetail = new OrderDetail();
                    orderDetail.Product = new Product();
                    orderDetail.Product.ID = discount.DiscountDiscountElementValue;
                    orderDetail.Cnt = Convert.ToInt32(discount.DiscountCount);
                    orderDetail.Price = 0;
                    orderMaster.OrderDetail.Add(orderDetail);
                }
                else if (discount.DiscountDiscountElementID == cnstDiscountElement.Euro)
                {
                    orderMaster.PriceDiscount = discount.DiscountCount;
                }
                else if (discount.DiscountDiscountElementID == cnstDiscountElement.Bonus)
                {
                    User user_Filter = new User();
                    user_Filter.ID = ((int)cnstDatabase.DBOperationResult.ValueIsNotEffective);
                    user_Filter.Contact = new Contact();
                    user_Filter.Contact.ID = orderMaster.Contact.ID;
                    user_Filter.LoginID = ((int)cnstDatabase.DBOperationResult.ValueIsNotEffective).ToString();
                    List<User> userList = CBLUser.Select(user_Filter);
                    
                    if (userList.Count > 0)
                    {
                        UserBonus userBonus_Filter = new UserBonus();
                        userBonus_Filter.ID = ((int)cnstDatabase.DBOperationResult.ValueIsNotEffective);
                        userBonus_Filter.Company = new Company();
                        userBonus_Filter.Company.ID = orderMaster.Company.ID;
                        userBonus_Filter.User = new User();
                        userBonus_Filter.User.ID = userList.First().ID;
                        List<UserBonus> userBonusList = CDAUserBonus.Select(userBonus_Filter);

                        if (userBonusList.Count > 0)
                        {
                            userBonusList.First().Cnt++;
                            res = CDAUserBonus.Update(userBonusList.First(), out errMsg);
                        }
                        else
                        {
                            UserBonus userBonus = new UserBonus();
                            userBonus.Company = new Company();
                            userBonus.Company.ID = orderMaster.Company.ID;
                            userBonus.User = new User();
                            userBonus.User.ID = userList.First().ID;
                            userBonus.Cnt = 1;
                            res = CDAUserBonus.Insert(userBonus, out errMsg);
                        }
                    }
                }
            }

            if (res)
            {
                if (CDAOrderMaster.Insert(orderMaster, out errMsg))
                {
                    foreach (OrderDetail orderdetail in orderMaster.OrderDetail)
                    {
                        orderdetail.OrderMaster = new OrderMaster();
                        orderdetail.OrderMaster.ID = orderMaster.ID;
                        if (CDAOrderDetail.Insert(orderdetail, out errMsg))
                        {
                            foreach (OrderDetailItem orderdetailitem in orderdetail.OrderDetailItem)
                            {
                                orderdetailitem.OrderDetail = new OrderDetail();
                                orderdetailitem.OrderDetail.ID = orderdetail.ID;
                                if (!CDAOrderDetailItem.Insert(orderdetailitem, out errMsg))
                                    res = false;
                            }
                        }
                        else
                        {
                            res = false;
                        }
                    }
                }
                else
                {
                    res = false;
                }
            }
            return res;
        }

    }
}
